package com.cherry.androidviewdemo.activity

import android.animation.ValueAnimator
import android.graphics.Color
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.animation.DecelerateInterpolator
import android.widget.LinearLayout
import android.widget.TextView
import com.cherry.androidviewdemo.R
import com.cherry.androidviewdemo.widget.ColorTextView
import kotlinx.android.synthetic.main.activity_darren_view.*

/**
 * @author DongMS
 * @since 2020/5/28
 */
class DarrenViewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_darren_view)
        initStepView()

        initColorTextView()

    }

    private fun initColorTextView() {
        val titles = arrayListOf("首页", "推荐", "榜单", "视频", "资讯")

        titles.forEach {
            val colorTextView = ColorTextView(this)
            colorTextView.changeTextColor = Color.BLUE
            colorTextView.text = it
            val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            lp.weight = 1f
            ll_tab.addView(colorTextView, lp)
        }

        val pagerAdapter = object : PagerAdapter() {

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                val textView = TextView(this@DarrenViewActivity)
                textView.gravity = Gravity.CENTER
                textView.text = titles[position]
                val lp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
                container.addView(textView, lp)
                return textView
            }

            override fun isViewFromObject(p0: View, p1: Any) = p0 == p1

            override fun getCount() = titles.size

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(`object` as View)
            }
        }

        view_pager.adapter = pagerAdapter
        view_pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {


            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

                when {
                    pagerAdapter.count == 1 -> {
                        //positionOffset 0-1
                        val colorTextView1 = ll_tab.getChildAt(0) as ColorTextView
                        colorTextView1.direction = ColorTextView.Direction.RIGHT_TO_LEFT
                        colorTextView1.progress = 1 - positionOffset
                    }
                    position == pagerAdapter.count - 1 -> {
                        //最后一个view单独处理
                        changeColorTextView(position, position - 1, positionOffset)
                    }
                    else -> {
                        changeColorTextView(position, position + 1, positionOffset)
                    }
                }
            }
        })
    }

    private fun changeColorTextView(startPosition: Int, endPosition: Int, positionOffset: Float) {
        //positionOffset 0-1
        val colorTextView1 = ll_tab.getChildAt(startPosition) as ColorTextView
        colorTextView1.direction = ColorTextView.Direction.RIGHT_TO_LEFT
        colorTextView1.progress = 1 - positionOffset

        val colorTextView2 = ll_tab.getChildAt(endPosition) as ColorTextView
        colorTextView2.direction = ColorTextView.Direction.LEFT_TO_RIGHT
        colorTextView2.progress = positionOffset
    }

    private fun initStepView() {
        step_view.maxStep = 4000

        val valueAnimator = ValueAnimator.ofInt(0, 3000)
        valueAnimator.addUpdateListener {
            val value = valueAnimator.animatedValue as Int
            step_view.currentStep = value
        }

        valueAnimator.interpolator = DecelerateInterpolator()
        valueAnimator.duration = 2000
        valueAnimator.start()
    }

    fun startProgress(view: View) {
        progress_view.maxValue = 100f
        val animator = ValueAnimator.ofFloat(0f, 80f)
        animator.addUpdateListener {
            val value = it.animatedValue as Float
            progress_view.progress = value
        }
        animator.interpolator = DecelerateInterpolator()
        animator.duration = 2000
        animator.start()
    }

    fun hideLoading(view: View) {
        loading_58_view.visibility = View.GONE
    }

//    fun leftToRight(view: View) {
//        color_text_view.direction = ColorTextView.Direction.LEFT_TO_RIGHT
//        val valueAnimator = ValueAnimator.ofFloat(0f, 1f)
//        valueAnimator.addUpdateListener {
//            val value = valueAnimator.animatedValue as Float
//            color_text_view.progress = value
//        }
//        valueAnimator.duration = 3000
//        valueAnimator.start()
//    }
//
//    fun rightToLeft(view: View) {
//        color_text_view.direction = ColorTextView.Direction.RIGHT_TO_LEFT
//        val valueAnimator = ValueAnimator.ofFloat(0f, 1f)
//        valueAnimator.addUpdateListener {
//            val value = valueAnimator.animatedValue as Float
//            color_text_view.progress = value
//        }
//        valueAnimator.duration = 3000
//        valueAnimator.start()
//    }

}